﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKArchUtils.Utils;

namespace YJKRevitDimensionText.OneKeyPlanDim.DimesionDataEx
{
	public class SplitLine
	{
		public SplitLine(Line lineSplit, List<Line> lstLine)
		{
			this.m_lstLine = lstLine;
			this.m_LineSplit = lineSplit;
			this.SplitOperator();
		}

		public List<Line> LstLineResult
		{
			get
			{
				return this.m_lstLineResult;
			}
		}

		private void SplitOperator()
		{
			try
			{
				Line line = this.m_LineSplit.Clone() as Line;
				int num = 0;
				foreach (Line line2 in this.m_lstLine)
				{
					XYZ xyzpoint = line.Project(line2.GetEndPoint(0)).XYZPoint;
					XYZ xyzpoint2 = line.Project(line2.GetEndPoint(1)).XYZPoint;
					XYZProject item = new XYZProject(xyzpoint, num);
					XYZProject item2 = new XYZProject(xyzpoint2, num);
					num++;
					this.m_lstXYZProject.Add(item);
					this.m_lstXYZProject.Add(item2);
				}
				this.m_lstXYZProject.Sort(new XYZProjectSort());
				List<XYZProject> list = new List<XYZProject>();
				list.Add(new XYZProject(this.m_LineSplit.GetEndPoint(0), -1));
				list.Add(new XYZProject(this.m_LineSplit.GetEndPoint(1), -1));
				list.Sort(new XYZProjectSort());
				Line item3 = null;
				if (this.CreateLineBy2Pts(list[0].m_ptPro, this.m_lstXYZProject[0].m_ptPro, out item3))
				{
					this.m_lstLineResult.Add(item3);
				}
				for (int i = 1; i < this.m_lstXYZProject.Count - 2; i++)
				{
					if (this.m_lstXYZProject[i].m_iCount != this.m_lstXYZProject[i + 1].m_iCount && this.CreateLineBy2Pts(this.m_lstXYZProject[i].m_ptPro, this.m_lstXYZProject[i + 1].m_ptPro, out item3))
					{
						this.m_lstLineResult.Add(item3);
					}
				}
				if (this.CreateLineBy2Pts(list[1].m_ptPro, this.m_lstXYZProject[this.m_lstXYZProject.Count - 1].m_ptPro, out item3))
				{
					this.m_lstLineResult.Add(item3);
				}
			}
			catch
			{
			}
		}

		private bool CreateLineBy2Pts(XYZ pt1, XYZ pt2, out Line lineNew)
		{
			lineNew = null;
			try
			{
				lineNew = Line.CreateBound(pt1, pt2);
				if (!Geometry.LessThan(lineNew.Length, Common.MMToFeet(1000.0)))
				{
					return true;
				}
			}
			catch
			{
			}
			return false;
		}

		private Line m_LineSplit;

		private List<Line> m_lstLine = new List<Line>();

		private List<XYZProject> m_lstXYZProject = new List<XYZProject>();

		private List<Line> m_lstLineResult = new List<Line>();
	}
}
